﻿using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections.Generic;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;
using HsTec.Mobile.Green.Util;
using Baimasoft.Manage.SqlServerDAL;


public partial class ReportAnalysis_FillSpace : System.Web.UI.Page
{
    EnergyProportionBLL bll = new EnergyProportionBLL();
    private string sEditStatus = String.Empty;
    Param MyP = new Param();
    List<EnergyProportion> list = new List<EnergyProportion>();
    private string sID = String.Empty;
    private string sArea = String.Empty;
    private string sYears = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] == null)
        {
            Response.Redirect("../Logon.aspx");
        }
        try
        {
            sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
            if (sEditStatus == "2")
            {
                sID = this.Request.QueryString["ID"].ToString().Trim();
                string[] ary = sID.Split('|');
                sArea = ary[0];
                sYears = ary[1];
            }
        }
        catch
        {

        }
        if (!IsPostBack)
        {
            BindsltArea();
            try
            {
                if (sEditStatus == "2")
                {
                    list = bll.GetModelList(" Area_ID = '" + sArea + "' and Years = '" + sYears + "'");
                    sltArea.Value = sArea;
                    sltYear.Value = sYears.Substring(0, 4);
                    sltMonth.Value = sYears.Substring(4, 2);
                    foreach (EnergyProportion ep in list)
                    {
                        TextBox tb = (TextBox)this.FindControl("txt" + ep.Subentry);
                        tb.Text = ep.Proportion.ToString();
                        HtmlInputCheckBox cb = (HtmlInputCheckBox)this.FindControl("chk" + ep.Subentry);
                        cb.Checked = ep.IsCalculat == "1" ? true : false;
                    }
                    sltArea.Disabled = true;
                    sltYear.Disabled = true;
                    sltMonth.Disabled = true;
                }
            }
            catch
            {

            }
        }
    }
    protected void SaveData_ServerClick(object sender, EventArgs e)
    {
        if (sEditStatus == "1")
        {
            sArea = sltArea.Value;
            sYears = sltYear.Value + sltMonth.Value;
        }
        string insertList = "Powers,Powers_1,Powers_1_1,Powers_1_2,Powers_1_2_1,Powers_1_3,Powers_2,Powers_2_1,Powers_3,Powers_3_1,Powers_3_1_1,Powers_3_1_2,Powers_3_1_3,Powers_3_2";
        string[] ary = insertList.Split(',');
        list.Clear();
        for (int i = 0; i < ary.Length; i++)
        {
            EnergyProportion model = new EnergyProportion();
            model.Area_ID = int.Parse(sArea);
            model.Years = sYears;
            model.AddDate = DateTime.Now;
            model.UpdateDate = DateTime.Now;
            model.UpdateUser = int.Parse(Session["Baimasoft_Pers_ID"].ToString());
            model.Subentry = ary[i];
            TextBox tb = (TextBox)this.FindControl("txt" + ary[i]);
            model.Proportion = Decimal.Parse(tb.Text.Trim());
            HtmlInputCheckBox cb = (HtmlInputCheckBox)this.FindControl("chk" + ary[i]);
            model.IsCalculat = cb.Checked == true ? "1" : "0";
            list.Add(model);
        }
        if (sEditStatus == "1")
        {
            if (bll.Exists("Area_ID='" + sltArea.Value + "' and Years='" + sltYear.Value + sltMonth.Value + "'"))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该记录已经存在，请修改区域或者年月！');</script>");
                return;
            }
            int iBack = bll.Add(list);
            if (iBack == 1)
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功！');window.location.href='FillSpace.aspx?EditStatus=1';</script>");
            else
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败！');</script>");
        }
        else if (sEditStatus == "2")
        {
            if (bll.Update(list, sArea, sYears) == 1)
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功！');window.location.href='FileSpaceManage.aspx';</script>");
            else
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败！');</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('传参错误！');</script>");
        }
    }
    private void BindsltArea()
    {
        sltArea.DataTextField = "Name";
        sltArea.DataValueField = "ID";
        sltArea.DataSource = bll.GetAreaList();
        sltArea.DataBind();
        sltArea.Items.Insert(0, "请选择县市区域...");
        sltArea.Items[0].Value = "";
    }
}
